package testcases;

import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

import manager.Manager;
import ball.Ball;
import board.Board;
import junit.framework.TestCase;

public class Results extends TestCase{
	private Manager manager;
	private Map<Point,Ball> ballArray=new HashMap<Point, Ball>();
    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
	public void setUpBallArray(Map<Point,Ball> ballArray, Ball[] balls){
		for(int i=0;i<balls.length;i++){
			ballArray.put(new Point(balls[i].getRow(),balls[i].getCol()),balls[i]);
		}
	}
	public void setUp() { 
		manager = new Manager(); 
		for(int i=0;i<6;i++){
			for(int j=0;j<6;j++){
				ballArray.put(new Point(i,j), null);
			}
		}
	}
  
	
	
	public boolean ballArrayEquals(Map<Point,Ball> ballArray, Map<Point,Ball> testBallArray){
		boolean result = true;
		for(int i=0;i<6;i++){
			for(int j=0;j<6;j++){
				Ball tempBall = testBallArray.get(new Point(i,j));
				Ball thisBall = ballArray.get(new Point(i,j));
				if(tempBall==null&&thisBall!=null)
					return false;
				else if(tempBall!=null&&thisBall==null)
					return false;
				else if(thisBall!=null &&tempBall!=null)
					 if(thisBall.getType()!=tempBall.getType())
						 return false;
			}
		}
		return result;
	}
	
	public void test_ballArrayEquals(){
		Map<Point,Ball> ballArray =new HashMap<Point, Ball>();
		Map<Point,Ball> testBallArray =new HashMap<Point, Ball>();
		Ball[] balls={new Ball(0,0,'4'),new Ball(0,5,'0')};
		this.setUpBallArray( ballArray,balls);
		Ball[] testBalls={new Ball(0,1,'4'),new Ball(0,5,'0')};
		for(int i=0;i<balls.length;i++){
			testBallArray.put(new Point(testBalls[i].getRow(),testBalls[i].getCol()),testBalls[i]);
		}
		boolean result = this.ballArrayEquals( ballArray, testBallArray);
		assertEquals(result,false);
	}
	public void test_ball1_col0() {
		boolean result;
		Ball[] balls={new Ball(0,0,'4'),new Ball(0,5,'0')};
		this.setUpBallArray( ballArray,balls);

		int col = 0;
		Board board = new Board();
		Ball ball = new Ball(0,0,'1');
		board.setBall(ball);
		board.chooseCol(col);
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}

	
	public void test_ball2_col0() {
		boolean result;
		Ball[] balls={new Ball(0,0,'4'),new Ball(0,5,'0')};
		this.setUpBallArray( ballArray,balls);
		int col = 0;
		Board board = new Board();
		Ball ball = new Ball(0,0,'2');
		board.setBall(ball);
		board.chooseCol(col);
		board.print();
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}
	public void test_ball3_col0() {
		boolean result;
		Ball[] balls={new Ball(0,0,'4'),new Ball(0,5,'0')};
		this.setUpBallArray( ballArray,balls);
		int col = 0;
		Board board = new Board();
		Ball ball = new Ball(0,0,'3');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);

	}

	public void test_ball5_col0() {
		boolean result;
		Ball[] balls={new Ball(0,0,'4'),new Ball(1,0,'5'),new Ball(0,5,'0')};
		this.setUpBallArray( ballArray,balls);
		int col = 0;
		Board board = new Board();
		Ball ball = new Ball(0,0,'5');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}
	
	public void test_ball1_col1() {
		boolean result;
		Ball[] balls={new Ball(0,0,'3'),new Ball(1,0,'4'),new Ball(0,5,'0')};
		this.setUpBallArray( ballArray,balls);

		int col = 1;
		Board board = new Board();
		Ball ball = new Ball(0,0,'1');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}
	public void test_ball2_col1() {
		boolean result;
		Ball[] balls={new Ball(0,0,'3'),new Ball(1,0,'4'),new Ball(0,5,'0')};
		this.setUpBallArray( ballArray,balls);

		int col = 1;
		Board board = new Board();
		Ball ball = new Ball(0,0,'2');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}
	public void test_ball3_col1() {
		boolean result;
		Ball[] balls={new Ball(0,0,'3'),new Ball(1,0,'4'),new Ball(0,5,'0'),new Ball(0,1,'3')};
		this.setUpBallArray( ballArray,balls);

		int col = 1;
		Board board = new Board();
		Ball ball = new Ball(0,0,'3');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}
	
	public void test_ball1_col2() {
		boolean result;
		Ball[] balls={new Ball(0,0,'3'),new Ball(1,0,'4'),new Ball(0,5,'0')};
		this.setUpBallArray( ballArray,balls);

		int col = 2;
		Board board = new Board();
		Ball ball = new Ball(0,0,'1');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}

	public void test_ball2_col2() {
		boolean result;
		Ball[] balls={new Ball(0,0,'3'),new Ball(1,0,'4'),new Ball(0,5,'0'),new Ball(0,2,'2')};
		this.setUpBallArray( ballArray,balls);

		int col = 2;
		Board board = new Board();
		Ball ball = new Ball(0,0,'2');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}
	
	public void test_ball3_col2() {
		boolean result;
		Ball[] balls={new Ball(0,0,'3'),new Ball(1,0,'4'),new Ball(0,5,'0'),new Ball(0,2,'3')};
		this.setUpBallArray( ballArray,balls);

		int col = 2;
		Board board = new Board();
		Ball ball = new Ball(0,0,'3');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertTrue(result);
	}
	
	public void test_Ball2_col5_specialBall() {
		boolean result;
		Ball[] balls={new Ball(0,0,'3'),new Ball(0,1,'4'),new Ball(0,5,'0'),new Ball(0,1,'2')};
		this.setUpBallArray( ballArray,balls);

		int col = 5;
		Board board = new Board();
		Ball ball = new Ball(0,0,'2');
		board.setBall(ball);
		board.chooseCol(col);
		
		Map<Point,Ball> testBallArray = this.ballArray;
		Map<Point,Ball> ballArray = board.getBallArray();
		result = this.ballArrayEquals(ballArray, testBallArray);
		assertEquals(result,false);
	}
	public void test_isGameOver() {
		boolean result;
		Ball[] balls={new Ball(0,0,'1'),new Ball(0,1,'1'),new Ball(0,2,'1'),new Ball(0,3,'1'),new Ball(0,4,'1'),new Ball(0,5,'1'),
				new Ball(1,0,'1'),new Ball(1,1,'1'),new Ball(1,2,'1'),new Ball(1,3,'1'),new Ball(1,4,'1'),new Ball(1,5,'1'),
				new Ball(2,0,'1'),new Ball(2,1,'1'),new Ball(2,2,'1'),new Ball(2,3,'1'),new Ball(2,4,'1'),new Ball(2,5,'1'),
				new Ball(3,0,'1'),new Ball(3,1,'1'),new Ball(3,2,'1'),new Ball(3,3,'1'),new Ball(3,4,'1'),new Ball(3,5,'1'),
				new Ball(4,0,'1'),new Ball(4,1,'1'),new Ball(4,2,'1'),new Ball(4,3,'1'),new Ball(4,4,'1'),new Ball(4,5,'1'),
				new Ball(5,0,'1'),new Ball(5,1,'1'),new Ball(5,2,'1'),new Ball(5,3,'1'),new Ball(5,4,'1'),new Ball(5,5,'1')};
		this.setUpBallArray( ballArray,balls);
		
		Board board = manager.getBoard();
		board.setBallArray(this.ballArray);
		int col=0;
		board.chooseCol(col);
		result = board.isGameOver();
		assertTrue(result);
		
	}

	
	

	
	
}
